﻿using System.Web.Security;
using AspNetMvc.Providers;

namespace AspNetMvc.Services.Authentication
{
   /// <summary>
   /// Membership service
   /// </summary>
   public class AccountMembershipService : IMembershipService
   {
      private OpenIdMembershipProvider _provider;

      /// <summary>
      /// Constructor
      /// </summary>
      public AccountMembershipService()
         : this(null)
      {
      }

      /// <summary>
      /// Constructor
      /// </summary>
      /// <param name="provider"></param>
      public AccountMembershipService(OpenIdMembershipProvider provider)
      {
         //if (provider == null)
         //{
         //   throw new ArgumentNullException("provider");
         //}

         _provider = provider ?? (OpenIdMembershipProvider)Membership.Provider;
      }

      /// <summary>
      /// Gets minimum password length
      /// </summary>
      public int MinPasswordLength
      {
         get
         {
            return _provider.MinRequiredPasswordLength;
         }
      }

      /// <summary>
      /// Validates user
      /// </summary>
      /// <param name="userName">Username</param>
      /// <param name="password">Password</param>
      /// <returns></returns>
      public bool ValidateUser(string userName, string password)
      {
         return _provider.ValidateUser(userName, password);
      }


      public bool ValidateUser(string openIdUrl, IOpenIdUserInfo info)
      {
         return _provider.ValidateUser(openIdUrl, info);
      }

      /// <summary>
      /// Creates user
      /// </summary>
      /// <param name="userName">Username</param>
      /// <param name="password">Password</param>
      /// <param name="email">E-mail address</param>
      /// <returns>Creation status</returns>
      public MembershipCreateStatus CreateUser(string username, string openId, string lang, IOpenIdUserInfo info)
      {
         MembershipCreateStatus status;
         _provider.CreateUser(username, openId, info, lang, out status);
         return status;
      }

      public MembershipUser ApproveUser(string confirmationKey)
      {
         return _provider.ApproveUser(confirmationKey);
      }

      /// <summary>
      /// Returns a username which is associated with the URL specified
      /// </summary>
      /// <param name="openIdUrl">OpenId URL</param>
      /// <returns></returns>
      public string GetUsernameFromOpenId(string openIdUrl, IOpenIdUserInfo info)
      {
         return _provider.GetUsernameFromOpenId(openIdUrl, info);
      }

      /// <summary>
      /// Creates user
      /// </summary>
      /// <param name="userName">Username</param>
      /// <param name="password">Password</param>
      /// <param name="email">E-mail address</param>
      /// <returns>Creation status</returns>
      public MembershipCreateStatus CreateUser(string userName, string password, string email, string lang, string confirmationLinkFormat, object providerUserKey)
      {
         MembershipCreateStatus status;
         _provider.CreateUser(userName, password, email, lang, confirmationLinkFormat, null, out status);
         return status;
      }

      /// <summary>
      /// Changes password
      /// </summary>
      /// <param name="userName">Username</param>
      /// <param name="oldPassword">Old password</param>
      /// <param name="newPassword">New password</param>
      /// <returns></returns>
      public bool ChangePassword(string userName, string oldPassword, string newPassword)
      {
         MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */);
         return currentUser.ChangePassword(oldPassword, newPassword);
      }
   }
}